package com.zhuge_new.Adapter;

import android.app.Activity;
import android.content.Context;
import android.preference.PreferenceActivity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.zhuge_new.Activity.PlanActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.ExamBean;
import com.zhuge_new.bean.PlanBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.NumberUtil;
import com.zhuge_new.untils.Tailuntils;

import java.util.Calendar;
import java.util.List;

/**
 * Created by lenovo on 2018-09-11.
 */

public class PlanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_TIME = 0;//带有日期的item
    public static final int TYPE_DATA = 1;//没有日期的item



    private Context context;
    private PlanBean planBean;


    public PlanAdapter(Context context, PlanBean planBean) {
        this.context=context;
        this.planBean=planBean;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TIME;
        } else {
            String currentDate = planBean.getRepayments().get(position).getCreateDate().substring(5, 7);
            int prevIndex = position - 1;
            boolean isDifferent = !planBean.getRepayments().get(prevIndex).getCreateDate().substring(5, 7).equals(currentDate);
            return isDifferent ? TYPE_TIME : TYPE_DATA;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_TIME) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_allplan1, parent, false);
            PlanViewHolder planViewHolder=new PlanViewHolder(view);
           return planViewHolder;
        } else if (viewType == TYPE_DATA) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_allplan2, parent, false);
           DataViewHolder dataViewHolder=new DataViewHolder(view);
           return dataViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (planBean == null)
            return;

        if (holder instanceof PlanViewHolder) {
            bindPlanItem((PlanViewHolder) holder, position);
        } else {
            bindDataItem((DataViewHolder) holder, position);
        }
    }

    private void bindPlanItem(final PlanViewHolder holder, final int position) {
        holder.all_pattern.setText(planBean.getRepayments().get(position).getRepaymentType());
        Double money= Double.valueOf(planBean.getRepayments().get(position).getRepaymentSum());
        String RepaymentSum=NumberUtil.saveRound(String.valueOf(money),2);
        holder.all_money.setText(RepaymentSum);
        String card=planBean.getRepayments().get(position).getCardNo();
        if (card.isEmpty()){
            holder.all_tail.setText("信用卡还款 尾号("+card.substring(card.length()-4,card.length())+")");
        }else {
            holder.all_tail.setText("信用卡还款 尾号("+card.substring(card.length()-4,card.length())+")");
        }
        String createDate=planBean.getRepayments().get(position).getRepaymentDay().substring(5,7);
        month(createDate,holder);
        holder.all_particulars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position ,  11);
            }
        });
        holder.all_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position ,12);
            }
        });

    }

    private void bindDataItem(final DataViewHolder holder, final int position) {
        holder.all_pattern.setText(planBean.getRepayments().get(position).getRepaymentType());
        Double money= Double.valueOf(planBean.getRepayments().get(position).getRepaymentSum());
        String RepaymentSum=NumberUtil.saveRound(String.valueOf(money),2);
        holder.all_money.setText(RepaymentSum);
        String card=planBean.getRepayments().get(position).getCardNo();
        if (card.isEmpty()){
            return;
        }else {
            holder.all_tail.setText("信用卡还款 尾号("+card.substring(card.length()-4,card.length())+")");
        }
        holder.all_particulars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position , 21);
            }
        });
        holder.all_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position ,22);
            }
        });
    }


    @Override
    public int getItemCount() {
        return planBean.getRepayments() ==null ? 0 :planBean.getRepayments().size();
    }

    public void removeData(int position) {
        planBean.getRepayments().remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, getItemCount());
    }


    public class PlanViewHolder extends RecyclerView.ViewHolder {

        private final TextView all_pattern;
        private final TextView all_tail;
        private final TextView all_money;
        private final TextView all_month;
        private final TextView all_particulars;
        private final TextView all_delete;

        public PlanViewHolder(View itemView) {
            super(itemView);
            all_pattern = itemView.findViewById(R.id.all_pattern);
            all_tail = itemView.findViewById(R.id.all_tail);
            all_money = itemView.findViewById(R.id.all_money);
            all_month = itemView.findViewById(R.id.all_month);
            all_particulars = itemView.findViewById(R.id.all_particulars);
            all_delete = itemView.findViewById(R.id.all_delete);
        }
    }

    public class DataViewHolder extends RecyclerView.ViewHolder{

        private final TextView all_pattern;
        private final TextView all_tail;
        private final TextView all_money;
        private final TextView all_particulars;
        private final TextView all_delete;

        public DataViewHolder(View itemView) {
            super(itemView);
            all_pattern = itemView.findViewById(R.id.all_pattern);
            all_tail = itemView.findViewById(R.id.all_tail);
            all_money = itemView.findViewById(R.id.all_money);
            all_particulars = itemView.findViewById(R.id.all_particulars);
            all_delete = itemView.findViewById(R.id.all_delete);
        }
    }

    public interface ItemClickListener {
        void onItemClick(int position,int i);
    }
    public ItemClickListener itemClickListener = null;

    public void setItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

    private void month(String createDate, PlanViewHolder holder) {

        //获取当前月
        String month;
        Calendar calendar = Calendar.getInstance();
        int current = calendar.get(Calendar.MONTH) + 1;
        if (current<10){
            month = "0"+String.valueOf(current);
        }else {
            month=String.valueOf(calendar);
        }

        switch (createDate){
            case "01":
                holder.all_month.setText("一月份计划");
                break;
            case "02":
                holder.all_month.setText("二月份计划");
                break;
            case "03":
                holder.all_month.setText("三月份计划");
                break;
            case "04":
                holder.all_month.setText("四月份计划");
                break;
            case "05":
                holder.all_month.setText("五月份计划");
                break;
            case "06":
                holder.all_month.setText("六月份计划");
                break;
            case "07":
                holder.all_month.setText("七月份计划");
                break;
            case "08":
                holder.all_month.setText("八月份计划");
                break;
            case "09":
                holder.all_month.setText("九月份计划");
                break;
            case "10":
                holder.all_month.setText("十月份计划");
                break;
            case "11":
                holder.all_month.setText("十一月份计划");
                break;
            case "12":
                holder.all_month.setText("十二月份计划");
                break;
        }
        if (month.equals(createDate)){
            holder.all_month.setText("本月计划");
        }
    }

}
